<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区" prop="district">
        <el-input
          v-model="queryParams.district"
          placeholder="请输入区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="街道" prop="street">
        <el-input
          v-model="queryParams.street"
          placeholder="请输入街道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面" prop="banner">
        <el-input
          v-model="queryParams.banner"
          placeholder="请输入封面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区名" prop="estateName">
        <el-input
          v-model="queryParams.estateName"
          placeholder="请输入小区名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区地址" prop="estateAddress">
        <el-input
          v-model="queryParams.estateAddress"
          placeholder="请输入小区地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成交均价" prop="avgPrice">
        <el-input
          v-model="queryParams.avgPrice"
          placeholder="请输入成交均价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成交周期" prop="tranCycle">
        <el-input
          v-model="queryParams.tranCycle"
          placeholder="请输入成交周期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="latitude" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入latitude"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="longitude" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入longitude"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ext1" prop="ext1">
        <el-input
          v-model="queryParams.ext1"
          placeholder="请输入ext1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ext2" prop="ext2">
        <el-input
          v-model="queryParams.ext2"
          placeholder="请输入ext2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ext3" prop="ext3">
        <el-input
          v-model="queryParams.ext3"
          placeholder="请输入ext3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yipaiju:house_estate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yipaiju:house_estate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yipaiju:house_estate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yipaiju:house_estate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="house_estateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="district" />
      <el-table-column label="街道" align="center" prop="street" />
      <el-table-column label="封面" align="center" prop="banner" />
      <el-table-column label="小区名" align="center" prop="estateName" />
      <el-table-column label="小区地址" align="center" prop="estateAddress" />
      <el-table-column label="成交均价" align="center" prop="avgPrice" />
      <el-table-column label="成交周期" align="center" prop="tranCycle" />
      <el-table-column label="位置" align="center" prop="position" />
      <el-table-column label="latitude" align="center" prop="latitude" />
      <el-table-column label="longitude" align="center" prop="longitude" />
      <el-table-column label="ext1" align="center" prop="ext1" />
      <el-table-column label="ext2" align="center" prop="ext2" />
      <el-table-column label="ext3" align="center" prop="ext3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yipaiju:house_estate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yipaiju:house_estate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="district">
          <el-input v-model="form.district" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="街道" prop="street">
          <el-input v-model="form.street" placeholder="请输入街道" />
        </el-form-item>
        <el-form-item label="封面" prop="banner">
          <el-input v-model="form.banner" placeholder="请输入封面" />
        </el-form-item>
        <el-form-item label="小区名" prop="estateName">
          <el-input v-model="form.estateName" placeholder="请输入小区名" />
        </el-form-item>
        <el-form-item label="小区地址" prop="estateAddress">
          <el-input v-model="form.estateAddress" placeholder="请输入小区地址" />
        </el-form-item>
        <el-form-item label="成交均价" prop="avgPrice">
          <el-input v-model="form.avgPrice" placeholder="请输入成交均价" />
        </el-form-item>
        <el-form-item label="成交周期" prop="tranCycle">
          <el-input v-model="form.tranCycle" placeholder="请输入成交周期" />
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="latitude" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入latitude" />
        </el-form-item>
        <el-form-item label="longitude" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入longitude" />
        </el-form-item>
        <el-form-item label="ext1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入ext1" />
        </el-form-item>
        <el-form-item label="ext2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入ext2" />
        </el-form-item>
        <el-form-item label="ext3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入ext3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouse_estate, getHouse_estate, delHouse_estate, addHouse_estate, updateHouse_estate } from "@/api/yipaiju/house_estate";

export default {
  name: "House_estate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区表格数据
      house_estateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,
        city: null,
        district: null,
        street: null,
        banner: null,
        estateName: null,
        estateAddress: null,
        avgPrice: null,
        tranCycle: null,
        position: null,
        latitude: null,
        longitude: null,
        ext1: null,
        ext2: null,
        ext3: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询小区列表 */
    getList() {
      this.loading = true;
      listHouse_estate(this.queryParams).then(response => {
        this.house_estateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        province: null,
        city: null,
        district: null,
        street: null,
        banner: null,
        estateName: null,
        estateAddress: null,
        avgPrice: null,
        tranCycle: null,
        position: null,
        latitude: null,
        longitude: null,
        ext1: null,
        ext2: null,
        ext3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小区";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHouse_estate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小区";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHouse_estate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse_estate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除小区编号为"' + ids + '"的数据项？').then(function() {
        return delHouse_estate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yipaiju/house_estate/export', {
        ...this.queryParams
      }, `house_estate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
